package com.example.jqying.androiddemo.recyclerView.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.jqying.androiddemo.R;

import java.util.List;

/**
 * Created by apple on 2018/6/24.
 */

public class WaterfallAdapter extends RecyclerView.Adapter<WaterfallAdapter.LinearViewHolder> {


    private Context mContext;
    private List<String > mList;
    private OnitemClickListener mlistener;


    /*
    *   上接LinearRecycleActivity
    *   使用流程
    *   1。构造时传入Context上下文
    *   2。设置RecycleView中item的数量
    *   3。设置每个Item的layout，通过LinearViewHolder
    *   4。linearViewHolder中的layout绑定到RecycleView中
    *   5。通过onBindView设置layout内部控件的属性
     */

    public WaterfallAdapter(Context context, OnitemClickListener listener){
        mContext = context;
        this.mlistener = listener;

    }

    @Override
    public LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_waterfall_item,parent,false ));
    }

    @Override
    public void onBindViewHolder(LinearViewHolder holder, final int position) {

        if(holder.imageView!=null) {



            if(position%9==0) {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529842378542&di=8bbc84e7b02f0b47606eec13c45d7f9b&imgtype=0&src=http%3A%2F%2Fi3.qhimg.com%2Ft01a2b6c37f0c48eec7.png").
                        into(holder.imageView);
            }else if(position%8==0) {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529837290827&di=0c9a501f475d3074a579e95d5b09c7cf&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01e2d859e9d42aa801202b0c75e4fa.jpg%402o.jpg").
                        into(holder.imageView);
            }

            else if(position%7==0) {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529837290828&di=f66e900eac9eae6272b2b79a5dbc06e3&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201604%2F06%2F20160406110323_hJjXM.jpeg").
                        into(holder.imageView);
            }
            else if(position%6==0) {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529833688184&di=900ba6b394d030c7a43c046a97faf82d&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fblog%2F201504%2F08%2F20150408091253_TeMvP.jpeg").
                        into(holder.imageView);
            }
            else if(position%5==0) {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529833688186&di=7196c0d39fbf34f0bf6102eacc64259f&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2Fafd8580fe30912402fc924369740537351360377.jpg").
                        into(holder.imageView);
            }
            else if(position%4==0) {
                Glide.with(mContext).
                        load("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=182938309,2010791406&fm=27&gp=0.jpg").
                        into(holder.imageView);
            }else if(position%3==0){
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529833688192&di=cc99ebeddf5587c83a75a6a55ba62195&imgtype=0&src=http%3A%2F%2Fa5.topitme.com%2Fo070%2F1007013000b95cf3d4.jpg").
                        into(holder.imageView);

            }else if(position%2==0){
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529833688111&di=3be5b8cdbe0201ff61985b17244634e3&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2F91ef76c6a7efce1b27893518a451f3deb58f6546.jpg").
                        into(holder.imageView);
            }
            else
            {
                Glide.with(mContext).
                        load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529833688193&di=4b01e82308f9a86c1b5eb2e5a4a7c1e7&imgtype=0&src=http%3A%2F%2Ff10.topitme.com%2Fl118%2F101185772310f36dec.jpg").
                        into(holder.imageView);
            }

        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //position 为触发事件的Item的编号
                //Toast.makeText(mContext,"Clicked : "+ position,Toast.LENGTH_SHORT).show();

                //调用用户定义的事件处理函数，类似C#的订阅和发布关系
                mlistener.onClick(position);
            }
        });

        //也可以处理其它事件，也可运用低耦合的接口
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return false;
            }
        });




    }

    @Override
    public int getItemCount() {
        return 30;
    }


    class LinearViewHolder extends RecyclerView.ViewHolder{

        private ImageView imageView;

        public LinearViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.iv_1);
        }
    }


    /*
    *   定义一个interface用于传入用户定义的事件
     */

    public interface OnitemClickListener{
        void onClick(int pos);
    }
}
